package com.beetlestech.nfe.main.biz.com.impl;
import java.util.ArrayList;
import java.util.List;

import com.beetlestech.nfe.fw.core.biz.AbstractDBLogic;
import com.beetlestech.nfe.fw.core.util.StringUtil;
import com.beetlestech.nfe.main.biz.com.HbVStudentListForTeacher;
import com.beetlestech.nfe.main.biz.com.dto.VstudentlistforteacherInfoDTO;
import com.beetlestech.nfe.main.biz.constant.CommonConstant;
import com.beetlestech.nfe.main.biz.constant.CtgCodeConstant;
import com.beetlestech.nfe.main.biz.constant.HbMessageCode;
import com.beetlestech.nfe.main.db.base.StatementDTO;
import com.beetlestech.nfe.main.db.base.StatementUtil;
import com.beetlestech.nfe.main.db.constant.SQLConstant;
import com.beetlestech.nfe.main.db.view.VstudentlistforteacherDTO;
public class HbVStudentListForTeacherImpl extends AbstractDBLogic implements HbVStudentListForTeacher, CtgCodeConstant, HbMessageCode, CommonConstant, SQLConstant {

	@Override
	public VstudentlistforteacherInfoDTO[] getStudentListForTeacher(
			Integer teacherId,
			String studentName,
			Integer courseTypeId
			) {
		Integer a=-1;
		List<VstudentlistforteacherInfoDTO> studentList = new ArrayList<VstudentlistforteacherInfoDTO>();
		List<VstudentlistforteacherInfoDTO> studentListReturn = new ArrayList<VstudentlistforteacherInfoDTO>();
		List<VstudentlistforteacherDTO> studentListDB = new ArrayList<VstudentlistforteacherDTO>();
		VstudentlistforteacherDTO vstudentlistDTO =new VstudentlistforteacherDTO();
		StatementDTO statementDTO = new StatementDTO();
		if(teacherId!=null && teacherId!=0){
			StatementUtil.andWhere(statementDTO, VstudentlistforteacherDTO.COLUMN_TEACHER_ID, StatementUtil.KEYWORD.EQUAL ,teacherId);

		}
		if(StringUtil.isNotEmpty(studentName)){
			StatementUtil.andWhere(statementDTO, VstudentlistforteacherDTO.COLUMN_NAME, StatementUtil.KEYWORD.LIKE ,studentName);
		}
		
		if(courseTypeId!=0){
			StatementUtil.andWhere(statementDTO, VstudentlistforteacherDTO.COLUMN_CATAGORY_C_ID, StatementUtil.KEYWORD.EQUAL ,courseTypeId);
		}
		vstudentlistDTO.set_dynamicWhereCondition(statementDTO.getSQLStatement());
		studentListDB=queryDAO.queryForObjectList(VstudentlistforteacherDTO.SQL_SEARCH, vstudentlistDTO);
		if(studentListDB==null || studentListDB.size()==0){
			createApplicationException("errMsg", "没找到要查询的信息");
		}else{
			for(VstudentlistforteacherDTO item:studentListDB){
				studentList.add(convertToVstudentListInfo(item));
			}
		}
       VstudentlistforteacherInfoDTO student=null;
       for (VstudentlistforteacherInfoDTO item : studentList) {
		if(item.getstudentid()==a){
			continue;
		}else{
			if(student!=null){
				studentListReturn.add(student);
			}
			student=new VstudentlistforteacherInfoDTO();
			student.setstudentid(item.getstudentid());
			student.setname(item.getname());
			a=item.getstudentid();
		}
		
	}
       if(student!=null){
			studentListReturn.add(student);
		}
	    return  studentListReturn.toArray(new VstudentlistforteacherInfoDTO[studentListReturn.size()]);
		
	}
	private VstudentlistforteacherInfoDTO convertToVstudentListInfo(VstudentlistforteacherDTO item){
		VstudentlistforteacherInfoDTO vstudentlistforteacherInfoDTO =new VstudentlistforteacherInfoDTO();
		vstudentlistforteacherInfoDTO.setstudentid(item.getstudentid());
		vstudentlistforteacherInfoDTO.setname(item.getname());
		return vstudentlistforteacherInfoDTO;
	}

	
}
